<?php
/**
 * @author qingyu8@staff.weibo.com
 * @date 2018/04/28
 */
namespace console\controllers;

use common\logic\Alipay;
use common\logic\OrderStatus;
use common\logic\PaymentStatus;
use common\logic\Order;
use common\logic\Trip;
use common\logic\User;
use common\models\Order as OrderModel;
use yii\console\Controller;
use yii;
use common\tool\Alarm;

set_time_limit(0);

class OrderController extends Controller {

    public function actionIndex() {
        echo __METHOD__;
    }

    public function actionCheckRefund() {
        $o = 0;
        $l = 20;
        while ($list = OrderModel::getColumn("order_status=:order_status AND payment_status=:payment_status", 'order_id', 'id', $o, $l, [
            ':order_status' => OrderStatus::REFUNDING,
            ':payment_status' => PaymentStatus::REFUNDING,
        ])) {
            foreach ($list as $item) {
                $order = Order::getOrderAdmin($item);
                $type = $order['payment']['type']??'';
                if ($type == Order::PAY_TYPE_ALI) {
                    Alipay::refundQuery($order);
                }
            }
            $o += $l;
        }
    }

    public function actionCreateQueue() {
        Yii::warning('start', __METHOD__);
        $i = 10;
        while ($i--) {
            $orderId = Yii::$app->redis->rpop(Yii::$app->params['redisKey']['createOrderQueue']);
            if ($orderId) {
                $order = Order::getOrderAdmin($orderId);
                Alarm::business('订单id：' . $orderId, '下单', $order['uid']);
                /* @var $app \EasyWeChat\OfficialAccount\Application */
                $app = Yii::$app->wechat->app;
                $subject = '趣潜 ' . Trip::getBoatName($order['trip_id']);
                //TODO 像客户发消息
                $openids = [
                    'o4IzE07WXptEK73Vn6nOLvnO1jAs',
                    //qingyu
                    'o4IzE00XREWnr8KXjEfhNFE_vCpo',
                    //guanting
                    'o4IzE09A7c6rTNc5HaR-nrd1w8OE',
                    //东红
                ];
                $username = $order['uid'] ? User::getNames([$order['uid']])[$order['uid']] : '';
                foreach ($openids as $openid) {
                    $app->template_message->send([
                        'touser' => $openid,
                        'template_id' => 'fwVY90GBChPp60piO-YecqPu00h-JbekAG3l_C0wrAU',
                        //'url' => 'https://easywechat.org',
                        'data' => [
                            'first' => 'Cooldive产生了一笔新订单 - 用户：' . $username,
                            'keyword1' => $subject,
                            'keyword2' => $orderId,
                            'keyword3' => count($order['contact']) . '人',
                            'keyword4' => '￥' . ($order['price'] / 100),
                            'remark' => '请关注该订单的状态。'
                        ],
                    ]);
                }
                $cnName = $order['contact'][0]['name'];
                $order['ext']['clientEnName'] = Order::clientEnName($cnName);
                OrderModel::update(['ext' => json_encode($order['ext'], JSON_UNESCAPED_UNICODE)], "order_id=$orderId");
            } else {
                sleep(5);
            }
        }
    }
}
